/** FASSIT (Forms of Selective Attention in Intelligent Transportation Systems)
 * Computational Creativity and Digital Media
 * Cognitive and Media Systems Group
 * Centre for Informatics and Systems of the University of Coimbra (CISUC)
 *
 * Copyright (c) 2010-2013 University of Coimbra, Portugal
 * All rights reserved.
 */
package agent.Knowledge;

import java.util.ArrayList;
import java.util.HashMap;

import poi.Info.POI;
import poi.Info.POIwithSurprise;
import poi.Surprise.Surprise;
import poi.Surprise.SurpriseDaysOff;
import poi.Surprise.SurprisePrice;
import poi.Surprise.SurpriseSchedule;
import poi.SurpriseCalculator.SurpriseCalculatorForPOI;
import agent.Context.Context;
import agent.DBManager.AgentDBManager;
import agent.Metrics.Surprise.PastSurpriseValues;

/**
 * This class is used as an Interface to the Agent methods. Allows the Agent to
 * store and retrieve information from the database. It also allows to convert a
 * POI to a POI with Surprise.
 * 
 * @author Hernani Costa
 * @version 0.1 date: 6/12/2012
 */
public class Agent {
	private AgentPastKnowledgeAboutSurprise agentPastKnowledgeAboutSurprise;
	private AgentDBManager dbManager;

	public Agent() {
		dbManager = new AgentDBManager();
	}

	/**
	 * Allows the agent to retrieve the agent's past knowledge about Surprise
	 * 
	 * @param context
	 *            - Context
	 * @return retrieves the agent's past knowledge about Surprise
	 */
	public Surprise getAgentPastKnowledgeAboutSurprise(
			Context context) {
		agentPastKnowledgeAboutSurprise = new AgentPastKnowledgeAboutSurprise(
				context);
		return agentPastKnowledgeAboutSurprise.getAgentPastKnowledgeAboutSurprise();
	}

	/**
	 * ------------------------------------------------------------------------
	 * Only used by the class Train. Deletes all the Agent's Knowledge
	 * ------------------------------------------------------------------------
	 */
	public void deleteAgentKnowledge() {
		dbManager.deleteAgentKnowledge();
	}

	/**
	 * ------------------------------------------------------------------------
	 * Stores all the POIs visited by the Agent
	 * ------------------------------------------------------------------------
	 * 
	 * @param pois
	 *            - POIs visited
	 * @param context
	 *            - context when the Agent visited these POIs
	 */
	public void savePOIs(ArrayList<POI> pois, Context context) {
		dbManager.savePOIs(pois, context);
	}

	/**
	 * ------------------------------------------------------------------------
	 * Saves the Surprise values of a specific POI, i.e., the Surprise values
	 * for all the parameters (Price, DaysOff and Schedule)
	 * ------------------------------------------------------------------------
	 * 
	 * @param poiWithSurprise
	 *            - POI with the surprise values
	 * @param context
	 *            - the Agent's context
	 */
	public void saveSurpriseValues(POIwithSurprise poiWithSurprise,
			Context context) {

		dbManager.saveSurpriseValues(poiWithSurprise, context);
	}

	/**
	 * ------------------------------------------------------------------------
	 * Returns all Agent's Knowledge for the surprise values already visited by
	 * the Agent, considering a specific Context (Goal)
	 * ------------------------------------------------------------------------
	 * 
	 * @param context
	 *            - Context
	 * @return Surprise values the Agent has
	 */
	public PastSurpriseValues getSurpriseValuesForGoal(Context context) {
		return dbManager.getSurpriseValuesForGoal(context);
	}

	/**
	 * This method creates the new object POIwithSurprise. It converts
	 * automatically a POI to a POIWithSurprise
	 * 
	 * @param poi
	 *            - the POI to be converted
	 * @return POIwithSurprise - the resulted POI with Surprise
	 */
	public POIwithSurprise getSurpriseValuesForPOI(POI poi) {
		HashMap<Integer, POI> pois = new HashMap<Integer, POI>();
		pois.put(poi.getPoiID(), poi);
		SurpriseCalculatorForPOI calculator = new SurpriseCalculatorForPOI(pois);
		HashMap<Integer, SurprisePrice> sPrice = calculator
				.getPoiSurprisePrice();
		HashMap<Integer, SurpriseDaysOff> sDayOff = calculator
				.getPoiSurpriseDayOff();
		HashMap<Integer, SurpriseSchedule> sSchedule = calculator
				.getPoiSurpriseSchedule();

		POIwithSurprise pSurprise = new POIwithSurprise(poi, sPrice.get(poi
				.getPoiID()), sDayOff.get(poi.getPoiID()), sSchedule.get(poi
				.getPoiID()));
		return pSurprise;
	}
}
